﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Automator.Tasks.Core;
using Automator.Tasks.Windows;

namespace Automator.Tasks
{
    /// <summary>
    /// Class that shows a MessageBox with customizable window title and text message.
    /// Attributes:
    /// Caption: window title text
    /// Text: message text
    /// </summary>
    public class InfoWindowTask: Task, ITaskExecutable
    {
        #region ITaskExecutable Members

        public void Execute(WorkflowExecutionContext context)
        {
            try
            {
                MessageBox.Show(
                    context.DereferenceVariables(this.Attributes["Text"].Value),
                    context.DereferenceVariables(this.Attributes["Caption"].Value),
                    MessageBoxButton.OK
                );
            }
            catch (Exception ex)
            {
                throw new TaskExecutionException(ex.Message, ex);
            }
        }

        #endregion        
        
        public InfoWindowTask():
            base()
        {
            this.Title = "Info Window";
            this.Description = "Display a message";
            this.TaskImage = new BitmapImage(new Uri("/Automator.Tasks;component/Resources/infowindow.png", UriKind.Relative));

            this.Attributes.Add(new TaskAttributeDefinition() { Key = "Caption", Value = String.Empty });
            this.Attributes.Add(new TaskAttributeDefinition() { Key = "Text", Value = String.Empty });
        }
        
        public InfoWindowTask(string header, string message): this()
        {
            this.Attributes["Caption"].Value = header;
            this.Attributes["Text"].Value = message;
        }
        
        public override string CommandText
        {
            get
            {
                return this.Attributes["Text"].Value.ToString().Trim();
            }
            set { base.CommandText = String.Empty; }
        }
        
        public override bool EditTask()
        {
            base.EditTask();
            InfoWindowTaskEditWindow window = new InfoWindowTaskEditWindow();
            window.Owner = Application.Current.MainWindow;
            window.Title = String.Format("Edit {0} Task", this.Title);
            //performing binding to the header
            window.header.ContentPanel.DataContext = this;

            window.edtWTitle.Text = this.Attributes["Caption"].Value.ToString();
            window.edtText.Text = this.Attributes["Text"].Value.ToString();
            window.footer.BtnOK.Click += delegate { window.DialogResult = true; };

            if (window.ShowDialog() == true)
            {
                this.Attributes["Caption"].Value = window.edtWTitle.Text.Trim();
                this.Attributes["Text"].Value = window.edtText.Text.Trim();
                this.CommandText = String.Empty;
                return true;
            }
            else return false;
        }
    }
}
